package com.m4f.business.model;

import java.io.Serializable;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.util.Date;
import javax.jdo.annotations.Persistent;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Company implements Serializable {
	
	private static final Logger LOGGER = Logger.getLogger(Company.class.getName());
	
	@PrimaryKey  
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent(defaultFetchGroup="true")
	@Embedded
	private CompanyInfo companyInfo = new CompanyInfo();
	
	@Persistent(defaultFetchGroup="true")
	@Embedded
	private Participant participantInfo = new Participant();
	
	@Persistent(mappedBy = "company", defaultFetchGroup="true")
	private List<Video> videos = new ArrayList<Video>();
	
	@NotPersistent
	private List<Vote> votes = new ArrayList<Vote>();
	
	@Persistent(defaultFetchGroup="true")
	private List<Document> documents = new ArrayList<Document>();

	@Persistent(defaultFetchGroup="true")
	@Embedded
	private AdditionalInformation additionalInformation = new AdditionalInformation();
	
	@Persistent
	private Boolean validated = false;
	
	@Persistent
	private Boolean preselected = false;
	
	@Persistent
	private Boolean finalist = false;
	
	@Persistent
	private Date createdDate;
	
	@Persistent
	private Date updatedDate; 
	
	
	public void setId(Long identifier) {
		this.id = identifier;
	}
	
	public Long getId() {
		return id;
	}

	public void setVideos(List<Video> videos) {
		this.videos = videos;
	}

	public List<Video> getVideos() {
		return this.videos;
	}

	public void setCompanyInfo(CompanyInfo companyInfo) {
		this.companyInfo = companyInfo;
	}

	public CompanyInfo getCompanyInfo() {
		return companyInfo;
	}

	public void setDocuments(List<Document> documents) {
		this.documents = documents;
	}

	public List<Document> getDocuments() {
		return documents;
	}

	public void setAdditionalInformation(AdditionalInformation additionalInformation) {
		this.additionalInformation = additionalInformation;
	}

	public AdditionalInformation getAdditionalInformation() {
		return additionalInformation;
	}

	public void setVotes(List<Vote> votes) {
		this.votes = votes;
	}

	public List<Vote> getVotes() {
		return votes;
	}
	
	public void setValidated(Boolean validated) {
		this.validated = validated;
	}

	public Boolean isValidated() {
		return validated;
	}
	
	public Boolean getValidated() {
		return validated;
	}

	public void setPreselected(Boolean preselected) {
		this.preselected = preselected;
	}

	public Boolean isPreselected() {
		return preselected;
	}
	
	public Boolean getPreselected() {
		return preselected;
	}

	public void setFinalist(Boolean finalist) {
		this.finalist = finalist;
	}
	
	public Boolean getFinalist() {
		return finalist;
	}
	
	public Boolean isFinalist() {
		return finalist;
	}
	
	public Participant getParticipantInfo() {
		return participantInfo;
	}

	public void setParticipantInfo(Participant participantInfo) {
		this.participantInfo = participantInfo;
	}
	
	public Boolean hasVideo() {
		if(this.videos == null) {
			return false;
		}
		if(this.videos.size()>0) {
			return true;
		}
		return false;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public Date getCreatedDate() {
		if(this.createdDate == null) {
			this.createdDate = Calendar.getInstance().getTime();
		}
		return createdDate;
	}
	
	public String getFormattedCreatedDate() {
		if(this.createdDate == null) {
			this.createdDate = Calendar.getInstance().getTime();
		}
		Format formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
		return formatter.format(createdDate);
	}

	public void setUpdatedDate(Date updatedDate) {
		this.updatedDate = updatedDate;
	}

	public Date getUpdatedDate() {
		if(this.updatedDate == null) {
			this.updatedDate = Calendar.getInstance().getTime();
		}
		return updatedDate;
	}
	
	public String getFormattedUpdatedDate() {
		if(this.updatedDate == null) {
			this.updatedDate = Calendar.getInstance().getTime();
		}
		Format formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
		return formatter.format(updatedDate);
	}
}